/** @file    ManBundle.cpp
 *  @author  Alessandro Polo
 *  @version $Id: ManBundle.cpp 2732 2010-07-06 03:18:11Z alex $
 *  @brief
 * File containing methods for the wosh::ManBundle class.
 * The header for this class can be found in ManBundle.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include "ManBundle.h"
 #include "ManImpl.h"

 #include <core/ObjectAllocator.h>
 #include <core/MessageFilter.h>
 #include <core/MethodsCommon.h>
 #include <core/MessageProcessingCommon.h>



using namespace std;
namespace wosh {
 namespace services {

 WOSH_REGISTER(wosh::services::ManBundle, "wosh::interfaces::services::Man", _Man_VERSION, _static_ManBundle1 )
 WOSH_REGISTER(wosh::services::ManBundle, "wosh::BundleGeneric", _Man_VERSION, _static_ManBundle2 )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

ManBundle::ManBundle()
	: BundleGeneric() {
	BundleGeneric::setName( _Man_NAME, false );
	Properties.update( _KEY_Version, _Man_VERSION );

//Log.setLevel(LOG_VERBOSE);

	Log(LOG_DEBUG, " Configuring Man worker.." );
	this->manWorker = new ManImpl(*this);

	Log(LOG_DEBUG, " Setting default properties and permissions.." );

	Properties.set( _Man_KEY_XmlFolder, "/shared/WOSH3/var/man/", Permission::RW_RW_R__() );
	Properties.set( _Man_KEY_XslFolder, "/shared/WOSH3/var/stylesheets/", Permission::RW_RW_R__() );
	Properties.set( _Man_KEY_TmpFolder, "/shared/WOSH3/tmp", Permission::RW_RW_R__() );

	Log(LOG_DEBUG, " Registering methods.." );

	MethodMessageResponse* mmMan = Methods.create<MethodMessageResponse>( _METHOD_Man, "man" );
	mmMan->setMethod( this, (MethodMessageResponsePtr)&ManBundle::mmDoMan );
	mmMan->getPermission().setMask( Permission::RX, Permission::RX, Permission::RX );

	MethodRequest* mmFlushCache = Methods.create<MethodRequest>( _Man_METHOD_flush );
	mmFlushCache->setMethod( this, (MethodRequestPtr)&ManBundle::mmDoFlushCache );
	mmFlushCache->getPermission().setMask( Permission::RX, Permission::RX, Permission::Read );

	MethodRequest* mmReGenerateCache = Methods.create<MethodRequest>( _Man_METHOD_regenerate );
	mmReGenerateCache->setMethod( this, (MethodRequestPtr)&ManBundle::mmDoReGenerateCache );
	mmReGenerateCache->getPermission().setMask( Permission::RX, Permission::RX, Permission::Read );

	setBundleState(Bundle::STATE_CREATED, false);
 }

ManBundle::~ManBundle() {
	Log(LOG_INFO, " Destroying.." );
	if ( isBundleRunning() ) {
		Log(LOG_WARNING, "~ManBundle() : Destroying while Bundle is running! Trying to stop.." );
		bundleStop();
	 }
	delete this->manWorker; this->manWorker = NULL;
	Log(LOG_VERBOSE, ":~ManBundle() : Destroyed." );
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////// BUNDLE CONTROL

WRESULT ManBundle::bundleStart() {
	if ( !BundleGeneric::bundleValidate_StartStop(Bundle::STATE_STARTING) ) return WRET_ERR_WRONG_STATE;

	WRESULT ret = BundleGeneric::start_SynchThread( this->manWorker );

	// BUNDLE-STATE (STARTED) will be updated async by WORKER, through call: thread_event()
	return ret;
 }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT ManBundle::bundleStop() {
	if ( !BundleGeneric::bundleValidate_StartStop(Bundle::STATE_STOPPING) ) return WRET_ERR_WRONG_STATE;

	WRESULT ret = BundleGeneric::stop_SynchThread( this->manWorker );

	// BUNDLE-STATE (STOPPED) will be updated async by WORKER, through call: thread_event()
	return ret;
 }

////////////////////////////////////////////////////////////////////////////////////////////// BUNDLE CONTROL
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void ManBundle::busMessage( const Message& message, const Bus* source ) {
	if ( message.isEmpty() ) return;
	if ( !message.getContent()->isRequest() )
		return;

	if ( !MessageFilter::isMessageTarget(message, this) ) {
		const Request* request = message.getContent()->asRequest();
		if ( request->getMethod() != _METHOD_Man )
			return;
		this->manWorker->appendRequest( message.clone() );
	 }
	else
		BundleGeneric::busMessage(message, source);
 }

////////////////////////////////////////////////////////////////////////////////////////////// PROPERTY EVENT


////////////////////////////////////////////////////////////////////////////////////////////// PROPERTY EVENT
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////// METHODS

Response* ManBundle::mmDoMan( const Message* requestMessage )
 {
	if ( requestMessage == NULL || requestMessage->isEmpty() ) return NULL;
	Log(LOG_VERBOSE, "mmDoMan()" );
	this->manWorker->appendRequest(requestMessage->clone());
	return NULL;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

Response* ManBundle::mmDoReGenerateCache( const Request* request )
 {
	if ( request == NULL ) return NULL;
	// retrieve options


	return NULL;
 }

Response* ManBundle::mmDoFlushCache( const Request* request )
 {
	if ( request == NULL ) return NULL;
	// retrieve options


	return NULL;
 }

///////////////////////////////////////////////////////////////////////////////////////////////////// METHODS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace services
}; // namespace wosh
